<?php
    session_start();
    if(!isset($_SESSION['loggedin'])) header('Location: index.php');
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="shortcut icon" href="../../assets/ico/favicon.ico">

    <title>TestYourSelf - Start</title>

    <!-- Bootstrap core CSS -->
    <link href="../../bootstrap/css/bootstrap.min.css" rel="stylesheet">

    <!-- Custom styles for this template -->
    <link href="css/result.css" rel="stylesheet">
    <link href="css/template.css" rel="stylesheet">

    <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
    <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body>

<div class="site-wrapper">

    <div class="site-wrapper-inner">

        <div class="cover-container">

            <div class="masthead clearfix">
                <div class="inner">
                    <h3 class="masthead-brand">TestYourSelf</h3>
                    <ul class="nav masthead-nav">
                        <li class="active"><a href="select.php">Start</a></li>
                        <li><a href="highscore.php">Highscore</a></li>
                        <li><a href="profil.php">Profil</a></li>
                        <li><a href="logout.php">Logout</a></li>
                        <?php if(isset($_SESSION['isadmin']) && $_SESSION['isadmin']=="true"){ ?>
                        <li><a href="admin.php">Administration</a></li>
                        <?php } ?>
                    </ul>
                </div>
            </div>


            <div class="inner cover">
                <h1 class="cover-heading">Ergebnis</h1>
                <form class="form-quiz" role="form" action="select.php" method="post">

                <?php
                if(!empty($_POST['questionlist'])){
                    $questions = explode("-", $_POST['questionlist']);
                    $count_right = 0;
                    $count_wrong = 0;
                    $count_notanswered = 0;
                    $points = 0;

                    $highscore = 0;

                    foreach($questions as $qid){

                        $questionurl= "http://localhost:8080/TestYourselfASE/rest/answer/".$qid;
                        $qfile= file_get_contents($questionurl);
                        $qobj = json_decode($qfile);

                ?>

                        <div class="question-heading"><?php echo $qobj->Name; ?></div>
                        <div class="question">
                        <?php
                                if (isset($_POST['optionsQ'.$qid])) {

                                    $resulturl = "http://localhost:8080/TestYourselfASE/rest/answer/".$_SESSION["userid"]."/".$_POST["scenario"]."/".$_POST["category"]."/".$qid."/".$_POST['optionsQ'.$qid];
                                    $resultfile = file_get_contents($resulturl);
                                    $rjson = json_decode($resultfile);
                                    $resultclass = "btnfalse";
                                    if (isset($rjson->richtig)){
                                        $resultclass = "btntrue";
                                        $count_right = $count_right + 1;
                                        $points = $points + $_POST['severityQ'.$qid];
                                    } else{

                                        $count_wrong = $count_wrong +1;
                                        $points = $points-1;
                                    }
                                }else $count_notanswered = $count_notanswered+1;
                        ?>
                            <div class="firstrow">
                                <label>
                                    <?php

                                    if (isset($_POST['optionsQ'.$qid]) && $_POST['optionsQ'.$qid] == 1)  {
                                        echo '<input type="radio" name="optionsQ'.$qid.'" id="optionsRadios2" value="1" class="radiob" disabled>';
                                        echo '<span class="btn btn-default '.$resultclass.'">'.$qobj->Option1.'</span>';
                                    }else{
                                        echo '<input type="radio" name="optionsQ'.$qid.'" id="optionsRadios2" value="1" class="radiob" disabled>';
                                        echo '<span class="btn btn-default">'.$qobj->Option1.'</span>';
                                    }
                                    ?>
                                </label>
                                <label >
                                    <?php

                                    if (isset($_POST['optionsQ'.$qid]) && $_POST['optionsQ'.$qid] == 2)  {
                                        echo '<input type="radio" name="optionsQ'.$qid.'" id="optionsRadios2" value="2" class="radiob" disabled>';
                                        echo '<span class="btn btn-default '.$resultclass.'">'.$qobj->Option2.'</span>';
                                    }else{
                                        echo '<input type="radio" name="optionsQ'.$qid.'" id="optionsRadios2" value="2" class="radiob" disabled>';
                                        echo '<span class="btn btn-default">'.$qobj->Option2.'</span>';
                                    }
                                    ?>
                                </label>
                            </div>
                            <div class="secondrow">
                                <label >
                                    <?php

                                    if (isset($_POST['optionsQ'.$qid]) && $_POST['optionsQ'.$qid] == 3)  {
                                        echo '<input type="radio" name="optionsQ'.$qid.'" id="optionsRadios2" value="3" class="radiob" disabled>';
                                        echo '<span class="btn btn-default '.$resultclass.'">'.$qobj->Option3.'</span>';
                                    }else{
                                        echo '<input type="radio" name="optionsQ'.$qid.'" id="optionsRadios2" value="3" class="radiob" disabled>';
                                        echo '<span class="btn btn-default">'.$qobj->Option3.'</span>';
                                    }
                                    ?>
                                </label>
                                <label >
                                    <?php

                                    if (isset($_POST['optionsQ'.$qid]) && $_POST['optionsQ'.$qid] == 4)  {
                                        echo '<input type="radio" name="optionsQ'.$qid.'" id="optionsRadios2" value="4" class="radiob" disabled>';
                                        echo '<span class="btn btn-default '.$resultclass.'">'.$qobj->Option4.'</span>';
                                    }else{
                                        echo '<input type="radio" name="optionsQ'.$qid.'" id="optionsRadios2" value="4" class="radiob" disabled>';
                                        echo '<span class="btn btn-default">'.$qobj->Option4.'</span>';
                                    }
                                    ?>
                                </label>
                            </div>
                        </div>

                <?php
                        }
                ?>
                <div class="statistic">
                    <h2>Insgesamt</h2>

                    <?php

                        $categorypointsurl = "http://localhost:8080/TestYourselfASE/rest/highscore/getHighscoreToCategory/".$_POST["scenario"]."/".$_POST["category"]."/".$_SESSION["userid"];
                        $categorypointsfile = file_get_contents($categorypointsurl);
                        $categorypoints = json_decode($categorypointsfile);

                        $highscore = $categorypoints->Points;

                        echo '<div class="nbrright">'.$count_right." richtige Antworten ( Punkte = Schwierigkeit )</div>";
                        echo '<div class="nbrwrong">'.$count_wrong." falsche Antworten ( -1 Punkt )</div>";
                        echo '<div class="nbrnotanswered">'.$count_notanswered." unbeantwortete Antworten ( 0 Punkte )</div>";
                        echo '<br />';
                        echo '<div class="points">Erreichte Punktzahl: '.$points.'</div>';
                        echo '<div class="points">Highscore: '.$highscore.'</div>';
                    ?>
                </div>
                        <button class="btn btn-lg btn-primary btn-block" type="submit">Beenden</button>
                <?php  }


                ?>

                    </form>

            </div>

            <div class="mastfoot">
                            <div class="inner">
                                <p>by Christoph Schuster, Martin Schmied, Thomas Stoellner, Philipp Taeubel</p>
                            </div>
                        </div>

        </div>

    </div>

</div>

<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="../../bootstrap/js/bootstrap.min.js"></script>
</body>
</html>

